package com.sline.test;

import java.io.*;
import java.util.TreeMap;

public class test2 {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader("abc.txt"));

        TreeMap<Character, Integer>tm = new TreeMap<>();

        int c;
        while ((c = br.read()) != -1) {
            char key = (char)c;
            tm.put(key, tm.containsKey(key) ? tm.get(key) + 1 : 1);
        }

        br.close();

        BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));

        for (Character key: tm.keySet()) {
            switch (key) {
                case '\t':
                    bw.write("\\t" + "=" + tm.get(key));
                    break;
                case '\r':
                    bw.write("\\r" + "=" + tm.get(key));
                    break;
                case '\n':
                    bw.write("\\n" + "=" + tm.get(key));
                    break;
                default:
                    bw.write(key + "=" + tm.get(key));
                    break;
            }

            bw.newLine();
        }

        bw.close();
    }

    /*
    * 获取一个文本上每个字符出现的次数, 将结果写在times.txt上
    *
    * 1.创建带缓冲的输入流对象;
    * 2.创建双列集合对象TreeMap
    * 3.将读到的字符存储在双列集合中, 存储的时候要做判断, 如果不包含这个键, 就将键和1存储, 如果包含这个键, 就将该键和值+1存储
    * 4.关闭输入流
    * 5.创建输出流对象
    * 6.遍历集合, 将集合中的内容写到times.txt中
    * 7.关闭输出流
    * */
}
